package com.xdm.resource.adapter.android;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;

import com.xdm.resource.XmResourceFactory;
import com.xdm.ui.XmImage;
import com.xdm.ui.XmItemPanelView;
import com.xdm.ui.adapter.android.AndroidImage;
import com.xdm.ui.adapter.android.AndroidItemPanelView;

/**
 * The factory for XM's Android UI adapters
 * 
 * @author Alexander Lapygin
 */
public class AndroidResourceFactory implements XmResourceFactory {
	private Activity activity;
	static private HashMap<Context, XmResourceFactory> instanceMap = new HashMap<Context, XmResourceFactory>();
	private HashMap<String, XmItemPanelView> viewMap = new HashMap<String, XmItemPanelView>();
	private HashMap<String, XmImage> imageMap = new HashMap<String, XmImage>();
	
	AndroidResourceFactory(Activity activity) {
		this.activity = activity;
	}

	public static XmResourceFactory getInstanse(Activity activity) {
		XmResourceFactory instance = (XmResourceFactory) instanceMap.get(activity);
		if(instance == null) {
			instance = new AndroidResourceFactory(activity);
			instanceMap.put(activity, instance);
		}
		return instance;
	}
	
	public XmItemPanelView getView(String id) {
		XmItemPanelView panelView = (XmItemPanelView) viewMap.get(id);
		if(panelView == null) {
			panelView = new AndroidItemPanelView(activity, id);
			viewMap.put(id, panelView);
		}
		return panelView;
	}

	public XmImage getImage(String id) {
		XmImage image = (XmImage) imageMap.get(id);
		if(image == null) {
			image = (XmImage) new AndroidImage(activity, id);
			imageMap.put(id, image);
		}
		return image;
	}
}
